import { request } from '../../../../services/request';

// 获取任务详情接口
export const getTaskDetailApi = async (batchOrderNum: string): Promise<any> => {
    console.log('=== 获取任务详情接口请求数据 ===');
    console.log('批次订单号:', batchOrderNum);
    console.log('======================');

    try {
        const response = await request('app/work/getTaskDetail', {
            method: 'POST',
            body: JSON.stringify({ work_param: batchOrderNum }),
        });

        console.log('=== 获取任务详情接口返回数据 ===');
        console.log('响应数据:', response);
        console.log('======================');

        return response;
    } catch (error) {
        console.error('=== 获取任务详情接口调用失败 ===');
        console.error('错误信息:', error);
        console.error('======================');
        throw error;
    }
};

// 获取工序任务详情接口
export const getProcessTaskDetailApi = async (batchOrderId: string, mbomId: string): Promise<any> => {
    console.log('=== 获取工序任务详情接口请求数据 ===');
    console.log('批次订单ID:', batchOrderId);
    console.log('MBOM ID:', mbomId);
    console.log('======================');

    try {
        const response = await request('app/work/getTaskDetail', {
            method: 'POST',
            body: JSON.stringify({
                batch_order_id: batchOrderId,
                mbom_id: mbomId
            }),
        });

        console.log('=== 获取工序任务详情接口返回数据 ===');
        console.log('响应数据:', response);
        console.log('======================');

        return response;
    } catch (error) {
        console.error('=== 获取工序任务详情接口调用失败 ===');
        console.error('错误信息:', error);
        console.error('======================');
        throw error;
    }
};

// 报工接口
export const pushWorkApi = async (params: {
    work_quantity: number;
    fatal_quantity: number;
    task_id: string;
}): Promise<any> => {
    console.log('=== 报工接口请求数据 ===');
    console.log('请求参数:', params);
    console.log('======================');

    try {
        const response = await request('app/work/pushWork', {
            method: 'POST',
            body: JSON.stringify(params),
        });

        console.log('=== 报工接口返回数据 ===');
        console.log('响应数据:', response);
        console.log('======================');

        return response;
    } catch (error) {
        console.error('=== 报工接口调用失败 ===');
        console.error('错误信息:', error);
        console.error('======================');
        throw error;
    }
};

// 进站接口
export const inStationApi = async (taskId: string): Promise<any> => {
    console.log('=== 进站接口请求数据 ===');
    console.log('任务ID:', taskId);
    console.log('======================');

    try {
        const response = await request('app/work/inStation', {
            method: 'POST',
            body: JSON.stringify({ task_id: taskId }),
        });

        console.log('=== 进站接口返回数据 ===');
        console.log('响应数据:', response);
        console.log('======================');

        return response;
    } catch (error) {
        console.error('=== 进站接口调用失败 ===');
        console.error('错误信息:', error);
        console.error('======================');
        throw error;
    }
};

// 出站接口
export const outStationApi = async (taskId: string): Promise<any> => {
    console.log('=== 出站接口请求数据 ===');
    console.log('任务ID:', taskId);
    console.log('======================');

    try {
        const response = await request('app/work/outStation', {
            method: 'POST',
            body: JSON.stringify({ task_id: taskId }),
        });

        console.log('=== 出站接口返回数据 ===');
        console.log('响应数据:', response);
        console.log('======================');

        return response;
    } catch (error) {
        console.error('=== 出站接口调用失败 ===');
        console.error('错误信息:', error);
        console.error('======================');
        throw error;
    }
};

// 获取报工列表接口
export const getWorkListApi = async (params: {
    batch_order_id: string;
    mbom_id: string;
}): Promise<any> => {
    console.log('=== 获取报工列表接口请求数据 ===');
    console.log('请求参数:', params);
    console.log('======================');

    try {
        const response = await request('app/work/getWorkList', {
            method: 'POST',
            body: JSON.stringify(params),
        });

        console.log('=== 获取报工列表接口返回数据 ===');
        console.log('响应数据:', response);
        console.log('======================');

        return response;
    } catch (error) {
        console.error('=== 获取报工列表接口调用失败 ===');
        console.error('错误信息:', error);
        console.error('======================');
        throw error;
    }
};

// 报检接口
export const pushIPQCApi = async (workId: string): Promise<any> => {
    console.log('=== 报检接口请求数据 ===');
    console.log('工作ID:', workId);
    console.log('======================');

    try {
        const response = await request('app/work/pushIPQC', {
            method: 'POST',
            body: JSON.stringify({ work_id: workId }),
        });

        console.log('=== 报检接口返回数据 ===');
        console.log('响应数据:', response);
        console.log('======================');

        return response;
    } catch (error) {
        console.error('=== 报检接口调用失败 ===');
        console.error('错误信息:', error);
        console.error('======================');
        throw error;
    }
};

// 设备呼叫接口
export const callDeviceApi = async (taskId: string): Promise<any> => {
    console.log('=== 设备呼叫接口请求数据 ===');
    console.log('任务ID:', taskId);
    console.log('======================');

    try {
        const response = await request('app/work/callDevice', {
            method: 'POST',
            body: JSON.stringify({ task_id: taskId }),
        });

        console.log('=== 设备呼叫接口返回数据 ===');
        console.log('响应数据:', response);
        console.log('======================');

        return response;
    } catch (error) {
        console.error('=== 设备呼叫接口调用失败 ===');
        console.error('错误信息:', error);
        console.error('======================');
        throw error;
    }
};

// 呼叫物流接口
export const callLogisticsApi = async (taskId: string): Promise<any> => {
    console.log('=== 呼叫物流接口请求数据 ===');
    console.log('任务ID:', taskId);
    console.log('======================');

    try {
        const response = await request('app/work/callLogistics', {
            method: 'POST',
            body: JSON.stringify({ task_id: taskId }),
        });

        console.log('=== 呼叫物流接口返回数据 ===');
        console.log('响应数据:', response);
        console.log('======================');

        return response;
    } catch (error) {
        console.error('=== 呼叫物流接口调用失败 ===');
        console.error('错误信息:', error);
        console.error('======================');
        throw error;
    }
};

// 绑定托盘接口
export const bindMesTrayApi = async (params: {
    batch_order_num: string;
    tray_num: string;
}): Promise<any> => {
    console.log('=== 绑定托盘接口请求数据 ===');
    console.log('请求参数:', params);
    console.log('======================');

    try {
        const response = await request('app/mesTray/bindMesTray', {
            method: 'POST',
            body: JSON.stringify(params),
        });

        console.log('=== 绑定托盘接口返回数据 ===');
        console.log('响应数据:', response);
        console.log('======================');

        return response;
    } catch (error) {
        console.error('=== 绑定托盘接口调用失败 ===');
        console.error('错误信息:', error);
        console.error('======================');
        throw error;
    }
};

// 意外呼叫接口
export const callAccidentApi = async (taskId: string): Promise<any> => {
    console.log('=== 意外呼叫接口请求数据 ===');
    console.log('任务ID:', taskId);
    console.log('======================');

    try {
        const response = await request('app/work/callAccident', {
            method: 'POST',
            body: JSON.stringify({ task_id: taskId }),
        });

        console.log('=== 意外呼叫接口返回数据 ===');
        console.log('响应数据:', response);
        console.log('======================');

        return response;
    } catch (error) {
        console.error('=== 意外呼叫接口调用失败 ===');
        console.error('错误信息:', error);
        console.error('======================');
        throw error;
    }
};

// 呼叫物料接口
export const callMaterialClaimApi = async (taskId: string): Promise<any> => {
    console.log('=== 呼叫物料接口请求数据 ===');
    console.log('任务ID:', taskId);
    console.log('======================');

    try {
        const response = await request('app/work/callMaterialClaim', {
            method: 'POST',
            body: JSON.stringify({ task_id: taskId }),
        });

        console.log('=== 呼叫物料接口返回数据 ===');
        console.log('响应数据:', response);
        console.log('======================');

        return response;
    } catch (error) {
        console.error('=== 呼叫物料接口调用失败 ===');
        console.error('错误信息:', error);
        console.error('======================');
        throw error;
    }
};

// 呼叫工装接口
export const callToolsClaimApi = async (taskId: string): Promise<any> => {
    console.log('=== 呼叫工装接口请求数据 ===');
    console.log('任务ID:', taskId);
    console.log('======================');

    try {
        const response = await request('app/work/callToolsClaim', {
            method: 'POST',
            body: JSON.stringify({ task_id: taskId }),
        });

        console.log('=== 呼叫工装接口返回数据 ===');
        console.log('响应数据:', response);
        console.log('======================');

        return response;
    } catch (error) {
        console.error('=== 呼叫工装接口调用失败 ===');
        console.error('错误信息:', error);
        console.error('======================');
        throw error;
    }
};

// 呼叫刀具接口
export const callKnifeClaimApi = async (taskId: string): Promise<any> => {
    console.log('=== 呼叫刀具接口请求数据 ===');
    console.log('任务ID:', taskId);
    console.log('======================');

    try {
        const response = await request('app/work/callKnifeClaim', {
            method: 'POST',
            body: JSON.stringify({ task_id: taskId }),
        });

        console.log('=== 呼叫刀具接口返回数据 ===');
        console.log('响应数据:', response);
        console.log('======================');

        return response;
    } catch (error) {
        console.error('=== 呼叫刀具接口调用失败 ===');
        console.error('错误信息:', error);
        console.error('======================');
        throw error;
    }
};

// 归还物料接口
export const callMaterialRefundApi = async (taskId: string): Promise<any> => {
    console.log('=== 归还物料接口请求数据 ===');
    console.log('任务ID:', taskId);
    console.log('======================');

    try {
        const response = await request('app/work/callMaterialRefund', {
            method: 'POST',
            body: JSON.stringify({ task_id: taskId }),
        });

        console.log('=== 归还物料接口返回数据 ===');
        console.log('响应数据:', response);
        console.log('======================');

        return response;
    } catch (error) {
        console.error('=== 归还物料接口调用失败 ===');
        console.error('错误信息:', error);
        console.error('======================');
        throw error;
    }
};

// 归还工装接口
export const callToolsRefundApi = async (taskId: string): Promise<any> => {
    console.log('=== 归还工装接口请求数据 ===');
    console.log('任务ID:', taskId);
    console.log('======================');

    try {
        const response = await request('app/work/callToolsRefund', {
            method: 'POST',
            body: JSON.stringify({ task_id: taskId }),
        });

        console.log('=== 归还工装接口返回数据 ===');
        console.log('响应数据:', response);
        console.log('======================');

        return response;
    } catch (error) {
        console.error('=== 归还工装接口调用失败 ===');
        console.error('错误信息:', error);
        console.error('======================');
        throw error;
    }
};

// 归还刀具接口
export const callKnifeRefundApi = async (taskId: string): Promise<any> => {
    console.log('=== 归还刀具接口请求数据 ===');
    console.log('任务ID:', taskId);
    console.log('======================');

    try {
        const response = await request('app/work/callKnifeRefund', {
            method: 'POST',
            body: JSON.stringify({ task_id: taskId }),
        });

        console.log('=== 归还刀具接口返回数据 ===');
        console.log('响应数据:', response);
        console.log('======================');

        return response;
    } catch (error) {
        console.error('=== 归还刀具接口调用失败 ===');
        console.error('错误信息:', error);
        console.error('======================');
        throw error;
    }
};

// 首检接口
export const pushFAIApi = async (taskId: string): Promise<any> => {
    console.log('=== 首检接口请求数据 ===');
    console.log('任务ID:', taskId);
    console.log('======================');

    try {
        const response = await request('app/work/pushFAI', {
            method: 'POST',
            body: JSON.stringify({ task_id: taskId }),
        });

        console.log('=== 首检接口返回数据 ===');
        console.log('响应数据:', response);
        console.log('======================');

        return response;
    } catch (error) {
        console.error('=== 首检接口调用失败 ===');
        console.error('错误信息:', error);
        console.error('======================');
        throw error;
    }
};

// 获取交接班列表接口
export const getMesTaskHandoverListApi = async (batchOrderId: string): Promise<any> => {
    console.log('=== 获取交接班列表接口请求数据 ===');
    console.log('批次订单ID:', batchOrderId);
    console.log('======================');

    try {
        const response = await request('app/mesTaskHandover/getMesTaskHandoverList', {
            method: 'POST',
            body: JSON.stringify({ batch_order_id: batchOrderId }),
        });

        console.log('=== 获取交接班列表接口返回数据 ===');
        console.log('响应数据:', response);
        console.log('======================');

        return response;
    } catch (error) {
        console.error('=== 获取交接班列表接口调用失败 ===');
        console.error('错误信息:', error);
        console.error('======================');
        throw error;
    }
};

// 保存或更新交接班接口
export const saveOrUpdateMesTaskHandoverApi = async (params: {
    task_id: string;
    remark: string;
    link_list: Array<{ link: string }>;
}): Promise<any> => {
    console.log('=== 保存或更新交接班接口请求数据 ===');
    console.log('请求参数:', params);
    console.log('======================');

    try {
        const response = await request('app/mesTaskHandover/saveOrUpdateMesTaskHandover', {
            method: 'POST',
            body: JSON.stringify(params),
        });

        console.log('=== 保存或更新交接班接口返回数据 ===');
        console.log('响应数据:', response);
        console.log('======================');

        return response;
    } catch (error) {
        console.error('=== 保存或更新交接班接口调用失败 ===');
        console.error('错误信息:', error);
        console.error('======================');
        throw error;
    }
}; 